package com.central.scheduler.worker.log;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply;
import com.central.scheduler.common.utils.LoggerUtils;

import static com.central.scheduler.common.utils.LoggerUtils.SQL_RESULT_DATA_NAME;
import static com.central.scheduler.common.utils.LoggerUtils.TASK_APPID_LOG_FORMAT;

/**
 * @author Tindy
 * @date 2022/1/5
 * @describe
 *  task log filter
 */
public class SqlResDataFilter extends Filter<ILoggingEvent> {

    /**
     * level
     */
    private Level level;

    public void setLevel(String level) {
        this.level = Level.toLevel(level);
    }

    /**
     * Accept or reject based on thread name
     * @param event event
     * @return FilterReply
     */
    @Override
    public FilterReply decide(ILoggingEvent event) {
        if (event.getThreadName().startsWith(LoggerUtils.TASK_LOGGER_THREAD_NAME)
                && event.getLoggerName().startsWith(SQL_RESULT_DATA_NAME)
                && (level==null || event.getLevel().isGreaterOrEqual(level))) {
            return FilterReply.ACCEPT;
        }
        return FilterReply.DENY;
    }
}